package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.CommentInfoDao;
import com.nf.dao.impl.CommentInfoDaoImpl;
import com.nf.entity.CommentInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servlet，实现添加评论
 */
@WebServlet("/api/CommentAddServlet.do")
public class CommentAddServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName=req.getParameter("userName");
        String image=req.getParameter("image");
        String filmName=req.getParameter("filmName");
        String content=req.getParameter("commentVal");
        int filmId= Integer.parseInt(req.getParameter("filmId"));
        int userId= Integer.parseInt(req.getParameter("userId"));

        CommentInfo comment=new CommentInfo();
        comment.setUserName(userName);
        comment.setImage(image);
        comment.setFilmName(filmName);
        comment.setContent(content);
        comment.setFilmId(filmId);
        comment.setUserId(userId);

        CommentInfoDao dao=new CommentInfoDaoImpl();
        int r= dao.add(comment);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if(r==1){
            out.print(new Gson().toJson(successJson("发表成功")));
        }else{
            out.print(new Gson().toJson(errorJson("发表失败")));
        }

        out.flush();
        out.close();
    }
}
